package mn.slide.ubjam.parser;

import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import mn.slide.ubjam.model.Point;
import mn.slide.ubjam.model.Road;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.util.Log;

public class RoadParser {
	/**
	 * @author Slide LTD [Б.Отгонбаяр]
	 * @since [2012-2-16]
	 * 
	 *        XML -с замуудын мэдээллийг parse хийх
	 */

	// Замуудын мэдээлэл агуулах Road төрлийн жагсаалт
	public static ArrayList<Road> roadList;

	/**
	 * 
	 * Өгөгдсөн XML -г parse хийн Road(Зам) төрлийн жагсаалтад оноох
	 * 
	 * @param xml
	 *            - Замуудын мэдээллийг агуулсан local XML файл
	 * @param context
	 *            - Програмын орчны мэдээлэл
	 * @return ArrayList<Road> - Road(Зам) төрлийн жагсаалт
	 */
	public static ArrayList<Road> parser(String xml, Context context) {
		roadList = new ArrayList<Road>();
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			DefaultHandler handler = new DefaultHandler() {

				boolean broadName = false;
				boolean bstartPoint = false;
				boolean bendPoint = false;
				boolean bx = false;
				boolean by = false;
				String roadName;
				String startPoint;
				String endPoint;
				ArrayList<Point> roadPoints;
				Point point;
				int x;
				int y;

				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					if (qName.equalsIgnoreCase("RoadName")) {
						broadName = true;
					}
					if (qName.equalsIgnoreCase("StartPoint")) {
						bstartPoint = true;
					}
					if (qName.equalsIgnoreCase("EndPoint")) {
						bendPoint = true;
					}
					if (qName.equalsIgnoreCase("x")) {
						bx = true;
					}
					if (qName.equalsIgnoreCase("y")) {
						by = true;
					}
					if (qName.equalsIgnoreCase("road")) {
						roadPoints = new ArrayList<Point>();
					}
				}

				public void endElement(String uri, String localName,
						String qName) throws SAXException {

					if (qName.equalsIgnoreCase("road")) {

						roadList.add(new Road(roadName, startPoint, endPoint,
								roadPoints));
						// Log.v(" ", " ");
						// Log.v("Parser", "roadName: [" + roadName + "]");
						// Log.v("Parser", "startPoint: [" + startPoint + "]");
						// Log.v("Parser", "endPoint: [" + endPoint + "]");
						// Log.v("Parser", "colorCode: [" + colorCode + "]");
					}

					if (qName.equalsIgnoreCase("point")) {
						point = new Point(x, y);
						roadPoints.add(point);
						// Log.v("Parser", "x: [" + x + "]");
						// Log.v("Parser", "y: [" + y + "]");
					}

					if (qName.equalsIgnoreCase("mapRoot")) {
						Log.v("Parser", "PARSE ХИЙЖ ДУУСАВ!!! Нийт: ["
								+ roadList.size() + "] ширхэг зам байна.");
					}
				}

				public void characters(char ch[], int start, int length)
						throws SAXException {
					if (broadName) {
						roadName = new String(ch, start, length);
						broadName = false;
					}
					if (bstartPoint) {
						startPoint = new String(ch, start, length);
						bstartPoint = false;
					}
					if (bendPoint) {
						endPoint = new String(ch, start, length);
						bendPoint = false;
					}
					if (bx) {
						x = Integer.parseInt(new String(ch, start, length));
						bx = false;
					}
					if (by) {
						y = Integer.parseInt(new String(ch, start, length));
						by = false;
					}
				}
			};
			saxParser.parse(context.getResources().getAssets().open(xml),
					handler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roadList;
	}
}
